/**
 * This class manages the warehouse
 */

/**
 * @author Maximilian Csuk
 * @author Bernhard Denner
 * @author Konrad Paumann
 *
 * $Revision$
 * $Date$
 */
import java.util.*;

public class WarehouseManager {
	/*
	 * variables
	 */
	private Warehouse warehouse;
	private Logger logger;
//	private User lockedBy;
	
	/*
	 * constructor
	 */
	public WarehouseManager() {
		warehouse = new Warehouse();
		logger = new Logger();
	}
	
	/**
	 * returns a product-handler for a specific product. Users should only change a product through a product-handler
	 * @param productGroupName
	 * @param product
	 * @param user
	 * @return
	 * @throws ProductException
	 */
	public ProductHandler getProductHandler(String productGroupName, String product, User user) throws ProductException {
		Product p = null;
		try {
			p = warehouse.getProductGroup(productGroupName).getProduct(product);
		} catch (ProductException e) {
			throw new ProductException("Kann keinen Produkt-Manager zurueckgeben: " + e.getMessage());
		}
		
		return new ProductHandler(p, user, logger);
	}
	
	/**
	 * returns a product-group-handler for a specific product-handler. Users should only change a product-group through a product-group-handler
	 * @param productGroupName
	 * @param product
	 * @param user
	 * @return a product-group-handler
	 * @throws ProductException
	 */
	public ProductGroupHandler getProductGroupHandler(String productGroupName, User user) throws ProductException {
		ProductGroup g = null;
		try {
			g = warehouse.getProductGroup(productGroupName);
		} catch (ProductException e) {
			throw new ProductException("Kann keinen Produktgruppen-Manager zurueckgeben: " + e.getMessage());
		}
		
		return new ProductGroupHandler(g, user, logger);
	}
	
	public WarehouseHandler getWarehouseHandler(User user) {
		return new WarehouseHandler(warehouse, user, logger);
	}
	
	public Logger getLogger() {
		return logger;
	}
	
//	public void lock(User user) throws UserException {
//		assert user != null;
//
//		if (lockedBy != null)
//			throw new UserException("Lagerverwaltung ist bereits gesperrt");
//		if (!user.canWrite())
//			throw new UserException("User nicht authorisiert");
//		
//		lockedBy = user;
//	}
//	
//	public void unlock(User user) throws UserException {
//		assert user != null;
//
//		if (lockedBy != user)
//			throw new UserException("User nicht authorisiert");
//		
//		lockedBy = null;
//	}
}
